<?php

App::uses('Controller', 'AdminAppController');

class AdminController extends AdminAppController {
	
	public $uses = array("Administrator", "Category", "City");
	
	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow(array('install', 'login'));
	}
	
	/**
	 * Performs an installation of initial data required to 
	 * use the application.
	 */
	public function install() {
		
		Configure::load('Admin.install');
		
		$data = Configure::read('install.data');
		
		foreach($data as $Model => $items) {
			foreach($items as $modelData)
				$this->$Model->save(array($Model => $modelData));
		}
		
		$this->setUserInfo( __('Installation complete.'), 'success' );
		
		$this->redirect('/admin/dashboard');
	}
	
	public function index() {
		if( !$this->Auth->user() ) {
			$this->redirect('login');
		}
		else {
			$this->redirect('dashboard');
		}
	}
	
	public function login() {
		if( $this->request->is('post') ) {
			if( $this->Auth->login() ) {
				$this->redirect( $this->Auth->redirect() );
			}
			else {
				$this->setUserInfo( __('Invalid username or password.'), 'error' );
			}
		}
	}
	
	public function logout() {
		
		$this->redirect( $this->Auth->logout() );
		
	}
	
	public function dashboard() {
		
	}
	
}